<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Hexo, NexT" />








  <link rel="shortcut icon" type="image/x-icon" href="/blog/favicon.ico?v=5.1.2" />






<meta name="description" content="1. 单例模式1.1. 条件 确保只有一个实例 可以全局访问  1.2. 适用 弹出框的实现 全局缓存  1.3. 实现12345678910111213141516const Singleton = function (name) &amp;#123;    this.name = name;&amp;#125;Singleton.prototype.getName = function () &amp;#123;">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaScript 之常用设计模式">
<meta property="og:url" content="http://xiezixi.oschina.io/2018/06/07/JS-常用设计模式/index.html">
<meta property="og:site_name" content="个人博客">
<meta property="og:description" content="1. 单例模式1.1. 条件 确保只有一个实例 可以全局访问  1.2. 适用 弹出框的实现 全局缓存  1.3. 实现12345678910111213141516const Singleton = function (name) &amp;#123;    this.name = name;&amp;#125;Singleton.prototype.getName = function () &amp;#123;">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-06-07T06:49:19.347Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JavaScript 之常用设计模式">
<meta name="twitter:description" content="1. 单例模式1.1. 条件 确保只有一个实例 可以全局访问  1.2. 适用 弹出框的实现 全局缓存  1.3. 实现12345678910111213141516const Singleton = function (name) &amp;#123;    this.name = name;&amp;#125;Singleton.prototype.getName = function () &amp;#123;">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://xiezixi.oschina.io/2018/06/07/JS-常用设计模式/"/>





  <title>JavaScript 之常用设计模式 | 个人博客</title>
  














</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">个人博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">曦瓜皮</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>


 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://xiezixi.oschina.io/blog/2018/06/07/JS-常用设计模式/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="曦瓜皮">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="个人博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">JavaScript 之常用设计模式</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-06-07T14:49:19+08:00">
                2018-06-07
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/JavaScript/" itemprop="url" rel="index">
                    <span itemprop="name">JavaScript</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="1-单例模式"><a href="#1-单例模式" class="headerlink" title="1. 单例模式"></a>1. 单例模式</h2><h3 id="1-1-条件"><a href="#1-1-条件" class="headerlink" title="1.1. 条件"></a>1.1. 条件</h3><ul>
<li>确保只有一个实例</li>
<li>可以全局访问</li>
</ul>
<h3 id="1-2-适用"><a href="#1-2-适用" class="headerlink" title="1.2. 适用"></a>1.2. 适用</h3><ul>
<li>弹出框的实现</li>
<li>全局缓存</li>
</ul>
<h3 id="1-3-实现"><a href="#1-3-实现" class="headerlink" title="1.3. 实现"></a>1.3. 实现</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> Singleton = <span class="function"><span class="keyword">function</span> (<span class="params">name</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.name = name;</span><br><span class="line">&#125;</span><br><span class="line">Singleton.prototype.getName = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>.name;</span><br><span class="line">&#125;</span><br><span class="line">Singleton.getInstance = <span class="function"><span class="keyword">function</span> (<span class="params">name</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (!<span class="keyword">this</span>.instance) &#123;</span><br><span class="line">        <span class="keyword">this</span>.instance = <span class="keyword">new</span> Singleton(name);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>.instance;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> a = Singleton.getInstance(<span class="string">'a'</span>);</span><br><span class="line"><span class="keyword">let</span> b = Singleton.getInstance(<span class="string">'b'</span>);</span><br><span class="line"><span class="built_in">console</span>.log(a === b); <span class="comment">// true</span></span><br><span class="line"><span class="built_in">console</span>.log(b.getName()); <span class="comment">// a</span></span><br></pre></td></tr></table></figure>
<p>因为 Javascript 是无类的语言，而 JavaScript 中的全局对象符合单例模式的两个条件，很多时候我们把全局对象当成单例模式使用：<code>var obj ={}</code>。<br>下面是用闭包实现的一个单例模式：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> CreateDiv = (<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> instance;</span><br><span class="line">    <span class="keyword">var</span> CreateDiv = <span class="function"><span class="keyword">function</span> (<span class="params">html</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (instance) &#123;</span><br><span class="line">            <span class="keyword">return</span> instance;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">this</span>.html = html;</span><br><span class="line">        <span class="keyword">this</span>.init();</span><br><span class="line">        <span class="keyword">return</span> instance = <span class="keyword">this</span>;</span><br><span class="line">    &#125;;</span><br><span class="line">    CreateDiv.prototype.init = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">var</span> div = <span class="built_in">document</span>.createElement(<span class="string">'div'</span>);</span><br><span class="line">        div.innerHTML = <span class="keyword">this</span>.html;</span><br><span class="line">        <span class="built_in">document</span>.body.appendChild(div);</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="keyword">return</span> CreateDiv;</span><br><span class="line">&#125;)();</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> a = <span class="keyword">new</span> CreateDiv(<span class="string">'sven1'</span>);</span><br><span class="line"><span class="keyword">var</span> b = <span class="keyword">new</span> CreateDiv(<span class="string">'sven2'</span>);</span><br><span class="line">alert(a === b); <span class="comment">// true</span></span><br></pre></td></tr></table></figure></p>
<p>高度抽象的单例模式代码，惰性单例的精髓：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 单例模式抽象，分离创建对象的函数和判断对象是否已经创建</span></span><br><span class="line"><span class="keyword">var</span> getSingle = <span class="function"><span class="keyword">function</span> (<span class="params">fn</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> result;</span><br><span class="line">    <span class="keyword">return</span> <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> result || ( result = fn.apply(<span class="keyword">this</span>, <span class="built_in">arguments</span>) );</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<h2 id="2-策略模式"><a href="#2-策略模式" class="headerlink" title="2. 策略模式"></a>2. 策略模式</h2><h3 id="2-1-定义"><a href="#2-1-定义" class="headerlink" title="2.1 定义"></a>2.1 定义</h3><p>根据不同参数可以命中不同的策略</p>
<h3 id="2-2-实现"><a href="#2-2-实现" class="headerlink" title="2.2. 实现"></a>2.2. 实现</h3><p>观察如下获取年终奖的 demo，根据不同的参数（level）获得不同策略方法(规则)，这是策略模式在 JS 比较经典的运用之一。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> strategy = &#123;</span><br><span class="line">    <span class="string">'S'</span>: <span class="function"><span class="keyword">function</span> (<span class="params">salary</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> salary * <span class="number">4</span>;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">'A'</span>: <span class="function"><span class="keyword">function</span> (<span class="params">salary</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> salary * <span class="number">3</span>;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">'B'</span>: <span class="function"><span class="keyword">function</span> (<span class="params">salary</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> salary * <span class="number">2</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> calculateBonus = <span class="function"><span class="keyword">function</span> (<span class="params">level, salary</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> strategy[level](salary);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">calculateBonus(<span class="string">'A'</span>, <span class="number">10000</span>); <span class="comment">// 30000</span></span><br></pre></td></tr></table></figure></p>
<p>在函数是一等公民的 JS 中，策略模式的使用常常隐藏在高阶函数中，稍微变换下上述 demo 的形式如下，可以发现我们平时已经在使用它了，恭喜我们又掌握了一种设计模式。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> S = <span class="function"><span class="keyword">function</span> (<span class="params">salary</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> salary * <span class="number">4</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> A = <span class="function"><span class="keyword">function</span> (<span class="params">salary</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> salary * <span class="number">3</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> B = <span class="function"><span class="keyword">function</span> (<span class="params">salary</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> salary * <span class="number">2</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> calculateBonus = <span class="function"><span class="keyword">function</span> (<span class="params">func, salary</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> func(salary);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">calculateBonus(A, <span class="number">10000</span>); <span class="comment">// 30000</span></span><br></pre></td></tr></table></figure></p>
<h3 id="2-3-优点"><a href="#2-3-优点" class="headerlink" title="2.3. 优点"></a>2.3. 优点</h3><ul>
<li>能减少大量 if 语句</li>
<li>复用性好</li>
</ul>
<h2 id="3-代理模式"><a href="#3-代理模式" class="headerlink" title="3. 代理模式"></a>3. 代理模式</h2><h3 id="3-1-情景"><a href="#3-1-情景" class="headerlink" title="3.1. 情景"></a>3.1. 情景</h3><p>情景：小明追女生 A</p>
<ul>
<li>非代理模式：小明 = 花 =&gt; 女生A</li>
<li>代理模式：小明 = 花 =&gt; 让女生A的好友B帮忙 = 花 =&gt; 女生A</li>
</ul>
<h3 id="3-2-特点"><a href="#3-2-特点" class="headerlink" title="3.2. 特点"></a>3.2. 特点</h3><p>代理对象和本体对象具有一致的接口，对使用者友好。<br>代理模式的种类有很多，在 JS 中最常用的为虚拟代理和缓存代理。</p>
<h3 id="3-3-实现"><a href="#3-3-实现" class="headerlink" title="3.3. 实现"></a>3.3. 实现</h3><p><strong>虚拟代理实现图片预加载</strong><br>下面这段代码运用代理模式来实现图片预加载，可以看到通过代理模式巧妙地将创建图片与预加载逻辑分离，并且在未来如果不需要预加载，只要改成请求本体代替请求代理对象就行。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> myImage = (<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">const</span> imgNode = <span class="built_in">document</span>.createElement(<span class="string">'img'</span>);</span><br><span class="line">    <span class="built_in">document</span>.body.appendChild(imgNode);</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        setSrc: <span class="function"><span class="keyword">function</span>(<span class="params">src</span>) </span>&#123;</span><br><span class="line">            imgNode.src = src;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)();</span><br><span class="line"><span class="keyword">const</span> proxyImage = (<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">const</span> img = <span class="keyword">new</span> Image();</span><br><span class="line">    img.onload = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123; <span class="comment">// http 图片加载完毕后才会执行</span></span><br><span class="line">        myImage.setSrc(<span class="keyword">this</span>.src);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        setSrc: <span class="function"><span class="keyword">function</span>(<span class="params">src</span>) </span>&#123;</span><br><span class="line">            myImage.setSrc(<span class="string">'loading.jpg'</span>); <span class="comment">// 本地 loading 图片</span></span><br><span class="line">            img.src = src;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)()</span><br><span class="line">proxyImage.setSrc(<span class="string">'http://loaded.jpg'</span>);</span><br></pre></td></tr></table></figure></p>
<p><strong>缓存代理实现乘积计算</strong><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> mult = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> a = <span class="number">1</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>, l; l = <span class="built_in">arguments</span>[i++];) &#123;</span><br><span class="line">        a = a * l</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> a</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> proxyMult = (<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">const</span> cache = &#123;&#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">const</span> tag = <span class="built_in">Array</span>.prototype.join.call(<span class="built_in">arguments</span>, <span class="string">','</span>)</span><br><span class="line">        <span class="keyword">if</span> (cache[tag]) &#123;</span><br><span class="line">            <span class="keyword">return</span> cache[tag]</span><br><span class="line">        &#125;</span><br><span class="line">        cache[tag] = mult.apply(<span class="keyword">this</span>, <span class="built_in">arguments</span>)</span><br><span class="line">        <span class="keyword">return</span> cache[tag]</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)()</span><br><span class="line"></span><br><span class="line">proxyMult(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>) <span class="comment">// 24</span></span><br></pre></td></tr></table></figure></p>
<h3 id="3-3-提示"><a href="#3-3-提示" class="headerlink" title="3.3. 提示"></a>3.3. 提示</h3><p>在开发时候不要先去猜测是否需要使用代理模式，如果发现直接使用某个对象不方便时，再来优化不迟。</p>
<h2 id="4-迭代器模式"><a href="#4-迭代器模式" class="headerlink" title="4. 迭代器模式"></a>4. 迭代器模式</h2><h3 id="4-1-定义"><a href="#4-1-定义" class="headerlink" title="4.1. 定义"></a>4.1. 定义</h3><p>能访问到聚合对象的顺序与元素</p>
<h3 id="4-2-实现"><a href="#4-2-实现" class="headerlink" title="4.2. 实现"></a>4.2. 实现</h3><p><strong>内部迭代器</strong><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">each</span>(<span class="params">arr, fn</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">        fn(i, arr[i]);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">each([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>], <span class="function"><span class="keyword">function</span>(<span class="params">i, n</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(i); <span class="comment">// 0 1 2</span></span><br><span class="line">    <span class="built_in">console</span>.log(n); <span class="comment">// 1 2 3</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure></p>
<p>可以看出内部迭代器在调用的时候非常简单，使用者不用关心迭代器内部实现的细节，但这也是内部迭代器的缺点。比如要比较两数组是否相等，只能在其回调函数中作文章了，代码如下：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> compare = <span class="function"><span class="keyword">function</span>(<span class="params">arr1, arr2</span>) </span>&#123;</span><br><span class="line">  each(arr1, <span class="function"><span class="keyword">function</span>(<span class="params">i, n</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (arr2[i] !== n) &#123;</span><br><span class="line">      <span class="built_in">console</span>.log(<span class="string">'两数组不等'</span>)</span><br><span class="line">      <span class="keyword">return</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">'两数组相等'</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> arr1 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line"><span class="keyword">const</span> arr2 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line">compare(arr1, arr2) <span class="comment">// 两数组相等</span></span><br></pre></td></tr></table></figure></p>
<p><strong>外部迭代器</strong><br>相较于内部迭代器，外部迭代器将遍历的权利转移到外部，因此在调用的时候拥有了更多的自由性，不过缺点是调用方式较复杂。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> iterator = <span class="function"><span class="keyword">function</span> (<span class="params">arr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> current = <span class="number">0</span></span><br><span class="line">    <span class="keyword">const</span> next = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        current = current + <span class="number">1</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">const</span> done = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> current &gt;= arr.length</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">const</span> value = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> arr[current]</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        next,</span><br><span class="line">        done,</span><br><span class="line">        value,</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> arr1 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line"><span class="keyword">const</span> arr2 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line"><span class="keyword">const</span> iterator1 = iterator(arr1)</span><br><span class="line"><span class="keyword">const</span> iterator2 = iterator(arr2)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> compare = <span class="function"><span class="keyword">function</span> (<span class="params">iterator1, iterator2</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">while</span> (!iterator1.done() &amp;&amp; !iterator2.done()) &#123;</span><br><span class="line">        <span class="keyword">if</span> (iterator1.value() !== iterator2.value()) &#123;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">'两数组不等'</span>)</span><br><span class="line">            <span class="keyword">return</span></span><br><span class="line">        &#125;</span><br><span class="line">        iterator1.next() <span class="comment">// 外部迭代器将遍历的权利转移到外部</span></span><br><span class="line">        iterator2.next()</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'两数组相等'</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">compare(iterator1, iterator2)</span><br></pre></td></tr></table></figure></p>
<h2 id="5-发布订阅模式"><a href="#5-发布订阅模式" class="headerlink" title="5. 发布订阅模式"></a>5. 发布订阅模式</h2><h3 id="5-1-说明"><a href="#5-1-说明" class="headerlink" title="5.1. 说明"></a>5.1. 说明</h3><p>事件发布/订阅模式 (PubSub) 在异步编程中帮助我们完成更松的解耦，甚至在 MVC、MVVC 的架构中以及设计模式中也少不了发布-订阅模式的参与。<br>优点：在异步编程中实现更深的解耦<br>缺点：如果过多的使用发布订阅模式，会增加维护的难度</p>
<h3 id="5-2-实现"><a href="#5-2-实现" class="headerlink" title="5.2. 实现"></a>5.2. 实现</h3><p><strong>实现一个发布订阅模式</strong><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> Event = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.obj = &#123;&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Event.prototype.on = <span class="function"><span class="keyword">function</span> (<span class="params">eventType, fn</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (!<span class="keyword">this</span>.obj[eventType]) &#123;</span><br><span class="line">        <span class="keyword">this</span>.obj[eventType] = []</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">this</span>.obj[eventType].push(fn)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Event.prototype.emit = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> eventType = <span class="built_in">Array</span>.prototype.shift.call(<span class="built_in">arguments</span>)</span><br><span class="line">    <span class="keyword">var</span> arr = <span class="keyword">this</span>.obj[eventType]</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">        arr[i].apply(arr[i], <span class="built_in">arguments</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> ev = <span class="keyword">new</span> Event()</span><br><span class="line"></span><br><span class="line">ev.on(<span class="string">'click'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">a</span>) </span>&#123; <span class="comment">// 订阅函数</span></span><br><span class="line">    <span class="built_in">console</span>.log(a) <span class="comment">// 1</span></span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">ev.emit(<span class="string">'click'</span>, <span class="number">1</span>)          <span class="comment">// 发布函数</span></span><br></pre></td></tr></table></figure></p>
<p><strong>订阅函数逻辑一定要优先于发布函数吗</strong><br>考虑以下场景：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">$.ajax(<span class="string">''</span>, () =&gt; &#123;</span><br><span class="line">  <span class="comment">// 异步订阅函数逻辑</span></span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 在其他地方执行发布函数，此时并不能保证执行发布函数的时候，订阅函数已经执行</span></span><br></pre></td></tr></table></figure></p>
<p>我们需要实现这样的逻辑：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> ev = <span class="keyword">new</span> Event()</span><br><span class="line">ev.emit(<span class="string">'click'</span>, <span class="number">1</span>)</span><br><span class="line"></span><br><span class="line">ev.on(<span class="string">'click'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">a</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(a) <span class="comment">// 1</span></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></p>
<p>目标明确后，来着手实现它：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> Event = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">this</span>.obj = &#123;&#125;</span><br><span class="line">  <span class="keyword">this</span>.cacheList = []</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Event.prototype.on = <span class="function"><span class="keyword">function</span>(<span class="params">eventType, fn</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (!<span class="keyword">this</span>.obj[eventType]) &#123;</span><br><span class="line">    <span class="keyword">this</span>.obj[eventType] = []</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">this</span>.obj[eventType].push(fn)</span><br><span class="line"></span><br><span class="line">  <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; <span class="keyword">this</span>.cacheList.length; i++) &#123;</span><br><span class="line">    <span class="keyword">this</span>.cacheList[i]()</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Event.prototype.emit = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">const</span> arg = <span class="built_in">arguments</span></span><br><span class="line">  <span class="keyword">const</span> that = <span class="keyword">this</span></span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">cache</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> eventType = <span class="built_in">Array</span>.prototype.shift.call(arg)</span><br><span class="line">    <span class="keyword">var</span> arr = that.obj[eventType]</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">      arr[i].apply(arr[i], arg)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">this</span>.cacheList.push(cache)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>以上代码实现思路就是把原本在 emit 里触发的函数存到 cacheList，再转交到 on 中触发。从而实现了发布函数先于订阅函数执行。</p>
<h2 id="6-命令模式"><a href="#6-命令模式" class="headerlink" title="6. 命令模式"></a>6. 命令模式</h2><p>命令模式与策略模式有些类似，在 JavaScript 中它们都是隐式的。<br>重要性：较低</p>
<h3 id="6-1-实现"><a href="#6-1-实现" class="headerlink" title="6.1. 实现"></a>6.1. 实现</h3><p>命令模式在 JavaScript 中也比较简单，下面代码中对按钮和命令进行了抽离，因此可以复杂项目中可以使用命令模式将界面的代码和功能的代码交付给不同的人去写。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> setCommand = <span class="function"><span class="keyword">function</span>(<span class="params">button, command</span>) </span>&#123;</span><br><span class="line">  button.onClick = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    command.excute()</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// --------------------  上面的界面逻辑由A完成，下面的由B完成</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> menu = &#123;</span><br><span class="line">  updateMenu: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'更新菜单'</span>)</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> UpdateCommand = <span class="function"><span class="keyword">function</span>(<span class="params">receive</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">    excute: receive.updateMenu,</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> updateCommand = UpdateCommand(menu) <span class="comment">// 创建命令</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> button1 = <span class="built_in">document</span>.getElementById(<span class="string">'button1'</span>)</span><br><span class="line">setCommand(button1, updateCommand)</span><br></pre></td></tr></table></figure></p>
<h2 id="11-中介者模式"><a href="#11-中介者模式" class="headerlink" title="11. 中介者模式"></a>11. 中介者模式</h2><h3 id="11-1-作用"><a href="#11-1-作用" class="headerlink" title="11.1 作用"></a>11.1 作用</h3><p>对象和对象之间借助第三方中介者进行通信。</p>
<h3 id="11-2-实现"><a href="#11-2-实现" class="headerlink" title="11.2. 实现"></a>11.2. 实现</h3><p>一场测试结束后，公布结果：告知解答出题目的人挑战成功，否则挑战失败。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> player = <span class="function"><span class="keyword">function</span> (<span class="params">name</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.name = name</span><br><span class="line">    playerMiddle.add(name)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">player.prototype.win = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    playerMiddle.win(<span class="keyword">this</span>.name)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">player.prototype.lose = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    playerMiddle.lose(<span class="keyword">this</span>.name)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> playerMiddle = (<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123; <span class="comment">// 将就用下这个 demo，这个函数当成中介者</span></span><br><span class="line">    <span class="keyword">const</span> players = []</span><br><span class="line">    <span class="keyword">const</span> winArr = []</span><br><span class="line">    <span class="keyword">const</span> loseArr = []</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        add: <span class="function"><span class="keyword">function</span> (<span class="params">name</span>) </span>&#123;</span><br><span class="line">            players.push(name)</span><br><span class="line">        &#125;,</span><br><span class="line">        win: <span class="function"><span class="keyword">function</span> (<span class="params">name</span>) </span>&#123;</span><br><span class="line">            winArr.push(name)</span><br><span class="line">            <span class="keyword">if</span> (winArr.length + loseArr.length === players.length) &#123;</span><br><span class="line">                <span class="keyword">this</span>.show()</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        lose: <span class="function"><span class="keyword">function</span> (<span class="params">name</span>) </span>&#123;</span><br><span class="line">            loseArr.push(name)</span><br><span class="line">            <span class="keyword">if</span> (winArr.length + loseArr.length === players.length) &#123;</span><br><span class="line">                <span class="keyword">this</span>.show()</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        show: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">let</span> winner <span class="keyword">of</span> winArr) &#123;</span><br><span class="line">                <span class="built_in">console</span>.log(winner + <span class="string">'挑战成功;'</span>)</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">let</span> loser <span class="keyword">of</span> loseArr) &#123;</span><br><span class="line">                <span class="built_in">console</span>.log(loser + <span class="string">'挑战失败;'</span>)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">    &#125;</span><br><span class="line">&#125;())</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> a = <span class="keyword">new</span> player(<span class="string">'A 选手'</span>)</span><br><span class="line"><span class="keyword">const</span> b = <span class="keyword">new</span> player(<span class="string">'B 选手'</span>)</span><br><span class="line"><span class="keyword">const</span> c = <span class="keyword">new</span> player(<span class="string">'C 选手'</span>)</span><br><span class="line"></span><br><span class="line">a.win()</span><br><span class="line">b.win()</span><br><span class="line">c.lose()</span><br><span class="line"></span><br><span class="line"><span class="comment">// A 选手挑战成功;</span></span><br><span class="line"><span class="comment">// B 选手挑战成功;</span></span><br><span class="line"><span class="comment">// C 选手挑战失败;</span></span><br></pre></td></tr></table></figure></p>
<p>在这段代码中 A、B、C 之间没有直接发生关系，而是通过另外的 playerMiddle 对象建立链接，姑且将之当成是中介者模式了。</p>
<h2 id="12-装饰者模式"><a href="#12-装饰者模式" class="headerlink" title="12. 装饰者模式"></a>12. 装饰者模式</h2><h3 id="12-1-作用"><a href="#12-1-作用" class="headerlink" title="12.1. 作用"></a>12.1. 作用</h3><p>动态地给函数赋能</p>
<h3 id="12-2-实现"><a href="#12-2-实现" class="headerlink" title="12.2. 实现"></a>12.2. 实现</h3><p>生活中的例子：天气冷了，就添加衣服来保暖；天气热了，就将外套脱下；这个例子很形象地含盖了装饰器的神韵，随着天气的冷暖变化，衣服可以动态的穿上脱下。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> wear = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'穿上第一件衣服'</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> _wear1 = wear</span><br><span class="line"></span><br><span class="line">wear = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    _wear1()</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'穿上第二件衣服'</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> _wear2 = wear</span><br><span class="line"></span><br><span class="line">wear = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    _wear2()</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'穿上第三件衣服'</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">wear()</span><br><span class="line"></span><br><span class="line"><span class="comment">// 穿上第一件衣服</span></span><br><span class="line"><span class="comment">// 穿上第二件衣服</span></span><br><span class="line"><span class="comment">// 穿上第三件衣服</span></span><br></pre></td></tr></table></figure></p>
<p>这种方式有以下缺点：1：临时变量会变得越来越多；2：this 指向有时会出错<br><strong>AOP 装饰函数</strong><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 前置代码</span></span><br><span class="line"><span class="built_in">Function</span>.prototype.before = <span class="function"><span class="keyword">function</span> (<span class="params">fn</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">const</span> self = <span class="keyword">this</span></span><br><span class="line">    <span class="keyword">return</span> <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        fn.apply(<span class="keyword">this</span>, <span class="built_in">arguments</span>)</span><br><span class="line">        <span class="keyword">return</span> self.apply(<span class="keyword">this</span>, <span class="built_in">arguments</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 后置代码</span></span><br><span class="line"><span class="built_in">Function</span>.prototype.after = <span class="function"><span class="keyword">function</span> (<span class="params">fn</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">const</span> self = <span class="keyword">this</span></span><br><span class="line">    <span class="keyword">return</span> <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        self.apply(<span class="keyword">this</span>, <span class="built_in">arguments</span>)</span><br><span class="line">        <span class="keyword">return</span> fn.apply(<span class="keyword">this</span>, <span class="built_in">arguments</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>用后置代码来实验下上面穿衣服的 demo，<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> wear1 = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'穿上第一件衣服'</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> wear2 = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'穿上第二件衣服'</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> wear3 = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'穿上第三件衣服'</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> wear = wear1.after(wear2).after(wear3)</span><br><span class="line">wear()</span><br><span class="line"></span><br><span class="line"><span class="comment">// 穿上第一件衣服</span></span><br><span class="line"><span class="comment">// 穿上第二件衣服</span></span><br><span class="line"><span class="comment">// 穿上第三件衣服</span></span><br></pre></td></tr></table></figure></p>
<p>但这样子有时会污染原生函数，可以做点通变<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> after = <span class="function"><span class="keyword">function</span> (<span class="params">fn, afterFn</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        fn.apply(<span class="keyword">this</span>, <span class="built_in">arguments</span>)</span><br><span class="line">        afterFn.apply(<span class="keyword">this</span>, <span class="built_in">arguments</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> wear = after(after(wear1, wear2), wear3)</span><br><span class="line">wear()</span><br></pre></td></tr></table></figure></p>
<h2 id="13-状态模式"><a href="#13-状态模式" class="headerlink" title="13. 状态模式"></a>13. 状态模式</h2><h3 id="13-1-定义"><a href="#13-1-定义" class="headerlink" title="13.1. 定义"></a>13.1. 定义</h3><p>将事物内部的每个状态分别封装成类，内部状态改变会产生不同行为。</p>
<ul>
<li>优点：用对象代替字符串记录当前状态，状态易维护</li>
<li>缺点：需编写大量状态类对象</li>
</ul>
<h3 id="13-2-实现"><a href="#13-2-实现" class="headerlink" title="13.2. 实现"></a>13.2. 实现</h3><p>某某牌电灯，按一下按钮打开弱光，按两下按钮打开强光，按三下按钮关闭灯光。<br><strong>面向对象实现的状态模式</strong><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 将状态封装成不同类</span></span><br><span class="line"><span class="keyword">const</span> weakLight = <span class="function"><span class="keyword">function</span> (<span class="params">light</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.light = light</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">weakLight.prototype.press = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'打开强光'</span>)</span><br><span class="line">    <span class="keyword">this</span>.light.setState(<span class="keyword">this</span>.light.strongLight)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> strongLight = <span class="function"><span class="keyword">function</span> (<span class="params">light</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.light = light</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">strongLight.prototype.press = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'关灯'</span>)</span><br><span class="line">    <span class="keyword">this</span>.light.setState(<span class="keyword">this</span>.light.offLight)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> offLight = <span class="function"><span class="keyword">function</span> (<span class="params">light</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.light = light</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">offLight.prototype.press = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'打开弱光'</span>)</span><br><span class="line">    <span class="keyword">this</span>.light.setState(<span class="keyword">this</span>.light.weakLight)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> Light = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.weakLight = <span class="keyword">new</span> weakLight(<span class="keyword">this</span>)</span><br><span class="line">    <span class="keyword">this</span>.strongLight = <span class="keyword">new</span> strongLight(<span class="keyword">this</span>)</span><br><span class="line">    <span class="keyword">this</span>.offLight = <span class="keyword">new</span> offLight(<span class="keyword">this</span>)</span><br><span class="line">    <span class="keyword">this</span>.currentState = <span class="keyword">this</span>.offLight          <span class="comment">// 初始状态</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Light.prototype.init = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">const</span> btn = <span class="built_in">document</span>.createElement(<span class="string">'button'</span>)</span><br><span class="line">    btn.innerHTML = <span class="string">'按钮'</span></span><br><span class="line">    <span class="built_in">document</span>.body.append(btn)</span><br><span class="line">    <span class="keyword">const</span> self = <span class="keyword">this</span></span><br><span class="line">    btn.addEventListener(<span class="string">'click'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        self.currentState.press()</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Light.prototype.setState = <span class="function"><span class="keyword">function</span> (<span class="params">state</span>) </span>&#123; <span class="comment">// 改变当前状态</span></span><br><span class="line">    <span class="keyword">this</span>.currentState = state</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> light = <span class="keyword">new</span> Light()</span><br><span class="line">light.init()</span><br><span class="line"></span><br><span class="line"><span class="comment">// 打开弱光</span></span><br><span class="line"><span class="comment">// 打开强光</span></span><br><span class="line"><span class="comment">// 关灯</span></span><br></pre></td></tr></table></figure></p>
<p><strong>非面向对象实现的状态模式</strong><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> obj = &#123;</span><br><span class="line">    <span class="string">'weakLight'</span>: &#123;</span><br><span class="line">        press: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">'打开强光'</span>)</span><br><span class="line">            <span class="keyword">this</span>.currentState = obj.strongLight</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">'strongLight'</span>: &#123;</span><br><span class="line">        press: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">'关灯'</span>)</span><br><span class="line">            <span class="keyword">this</span>.currentState = obj.offLight</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">'offLight'</span>: &#123;</span><br><span class="line">        press: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">'打开弱光'</span>)</span><br><span class="line">            <span class="keyword">this</span>.currentState = obj.weakLight</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> Light = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.currentState = obj.offLight</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Light.prototype.init = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">const</span> btn = <span class="built_in">document</span>.createElement(<span class="string">'button'</span>)</span><br><span class="line">    btn.innerHTML = <span class="string">'按钮'</span></span><br><span class="line">    <span class="built_in">document</span>.body.append(btn)</span><br><span class="line">    <span class="keyword">const</span> self = <span class="keyword">this</span></span><br><span class="line">    btn.addEventListener(<span class="string">'click'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        self.currentState.press.call(self) <span class="comment">// 通过 call 完成委托</span></span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> light = <span class="keyword">new</span> Light()</span><br><span class="line">light.init()</span><br></pre></td></tr></table></figure></p>
<h2 id="14-适配者模式"><a href="#14-适配者模式" class="headerlink" title="14. 适配者模式"></a>14. 适配者模式</h2><h3 id="14-1-作用"><a href="#14-1-作用" class="headerlink" title="14.1. 作用"></a>14.1. 作用</h3><p>主要用于解决两个接口之间不匹配的问题。</p>
<h3 id="14-2-实现"><a href="#14-2-实现" class="headerlink" title="14.2. 实现"></a>14.2. 实现</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> zheJiangCityOld = (<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> [</span><br><span class="line">        &#123;</span><br><span class="line">            name: <span class="string">'hangzhou'</span>,</span><br><span class="line">            id: <span class="number">11</span>,</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            name: <span class="string">'jinhua'</span>,</span><br><span class="line">            id: <span class="number">12</span>,</span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;)();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 新接口期望的类型 &#123; hangzhou: 11,jinhua: 12 &#125;</span></span><br><span class="line"><span class="keyword">const</span> adaptor = (<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">const</span> obj = &#123;&#125;;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> city <span class="keyword">of</span> zheJiangCityOld) &#123;</span><br><span class="line">        obj[city.name] = city.id;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> obj;</span><br><span class="line">&#125;)();</span><br><span class="line"><span class="built_in">console</span>.log(adaptor); <span class="comment">// &#123; hangzhou: 11,jinhua: 12 &#125;</span></span><br></pre></td></tr></table></figure>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/06/06/JS-原型链/" rel="next" title="JavaScript 之原型到原型链™">
                <i class="fa fa-chevron-left"></i> JavaScript 之原型到原型链™
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/06/07/ES6-Promise/" rel="prev" title="ECMAScript6 之 Promise™">
                ECMAScript6 之 Promise™ <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/blog/images/avatar.jpg"
               alt="曦瓜皮" />
          <p class="site-author-name" itemprop="name">曦瓜皮</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/blog/archives/">
                <span class="site-state-item-count">15</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/blog/categories/index.html">
                <span class="site-state-item-count">6</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        

      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-单例模式"><span class="nav-number">1.</span> <span class="nav-text">1. 单例模式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-条件"><span class="nav-number">1.1.</span> <span class="nav-text">1.1. 条件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-适用"><span class="nav-number">1.2.</span> <span class="nav-text">1.2. 适用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-实现"><span class="nav-number">1.3.</span> <span class="nav-text">1.3. 实现</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-策略模式"><span class="nav-number">2.</span> <span class="nav-text">2. 策略模式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-定义"><span class="nav-number">2.1.</span> <span class="nav-text">2.1 定义</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-实现"><span class="nav-number">2.2.</span> <span class="nav-text">2.2. 实现</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-优点"><span class="nav-number">2.3.</span> <span class="nav-text">2.3. 优点</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-代理模式"><span class="nav-number">3.</span> <span class="nav-text">3. 代理模式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-情景"><span class="nav-number">3.1.</span> <span class="nav-text">3.1. 情景</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-特点"><span class="nav-number">3.2.</span> <span class="nav-text">3.2. 特点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-实现"><span class="nav-number">3.3.</span> <span class="nav-text">3.3. 实现</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-提示"><span class="nav-number">3.4.</span> <span class="nav-text">3.3. 提示</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-迭代器模式"><span class="nav-number">4.</span> <span class="nav-text">4. 迭代器模式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1-定义"><span class="nav-number">4.1.</span> <span class="nav-text">4.1. 定义</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-实现"><span class="nav-number">4.2.</span> <span class="nav-text">4.2. 实现</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-发布订阅模式"><span class="nav-number">5.</span> <span class="nav-text">5. 发布订阅模式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-1-说明"><span class="nav-number">5.1.</span> <span class="nav-text">5.1. 说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-2-实现"><span class="nav-number">5.2.</span> <span class="nav-text">5.2. 实现</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-命令模式"><span class="nav-number">6.</span> <span class="nav-text">6. 命令模式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#6-1-实现"><span class="nav-number">6.1.</span> <span class="nav-text">6.1. 实现</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#11-中介者模式"><span class="nav-number">7.</span> <span class="nav-text">11. 中介者模式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#11-1-作用"><span class="nav-number">7.1.</span> <span class="nav-text">11.1 作用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#11-2-实现"><span class="nav-number">7.2.</span> <span class="nav-text">11.2. 实现</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#12-装饰者模式"><span class="nav-number">8.</span> <span class="nav-text">12. 装饰者模式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#12-1-作用"><span class="nav-number">8.1.</span> <span class="nav-text">12.1. 作用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#12-2-实现"><span class="nav-number">8.2.</span> <span class="nav-text">12.2. 实现</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#13-状态模式"><span class="nav-number">9.</span> <span class="nav-text">13. 状态模式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#13-1-定义"><span class="nav-number">9.1.</span> <span class="nav-text">13.1. 定义</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#13-2-实现"><span class="nav-number">9.2.</span> <span class="nav-text">13.2. 实现</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#14-适配者模式"><span class="nav-number">10.</span> <span class="nav-text">14. 适配者模式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#14-1-作用"><span class="nav-number">10.1.</span> <span class="nav-text">14.1. 作用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#14-2-实现"><span class="nav-number">10.2.</span> <span class="nav-text">14.2. 实现</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">曦瓜皮</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.2"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.2"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.2"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.2"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.2"></script>



  


  




	





  





  






  





  

  

  

  

  

  

</body>
</html>
